    //
//  SignupViewController.m
//  SpendFunk
//
//  Created by dongmh on 4/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SignupViewController.h"
#import "SpendFunkAppDelegate.h"
#import "image.h"
#import "Global.h"
#import "UIView+firstResponder.h"
@implementation SignupViewController
@synthesize nextPreviousControl;
@synthesize keyboardToolbar;


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)init
{
	isUpdate = FALSE;
	return self;
}

-(void)loadView
{
	[super loadView];
	[self initView];
}
-(void)initView
{
	//
	
	NSString *path = [[NSString alloc] initWithFormat:@"%@",IMG_HEADER];
	UIImage *img = [UIImage imageNamed:path];
	UIImageView *imgvBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
	[imgvBar setImage:img];
	[self.view addSubview:imgvBar];
	[imgvBar setUserInteractionEnabled:YES];
	[imgvBar release];
	
	//back button
	[path release];
	//[img release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_BACK];
	img = [UIImage imageNamed:path];
	m_pBtnBack = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, img.size.width, img.size.height)];
	[m_pBtnBack setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnBack setTitle:@"Back" forState:UIControlStateNormal];
	[m_pBtnBack setBackgroundColor:[UIColor clearColor]];
	m_pBtnBack.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnBack addTarget:nil action:@selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnBack];
	
	//done button
	[path release];
	//[img release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_NEXT];
	img = [UIImage imageNamed:path];
	m_pBtnDone = [[UIButton alloc] initWithFrame:CGRectMake(320 - 5 - img.size.width, 5, img.size.width, img.size.height)];
	[m_pBtnDone setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnDone setBackgroundColor:[UIColor clearColor]];
	[m_pBtnDone setTitle:@"Done" forState:UIControlStateNormal];
	m_pBtnDone.titleLabel.textColor = [UIColor whiteColor];
	[m_pBtnDone addTarget:nil action:@selector(onClickDone) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:m_pBtnDone];
	
 	//
	//[path release];
//	//[img release];
//	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_SIGNUP];
//	img = [UIImage imageNamed:path];
//	UIImageView *imgvSingupBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0,45, img.size.width, img.size.height)];
//	[imgvSingupBkgr setImage:img];
//	[self.view addSubview:imgvSingupBkgr];
//	[imgvSingupBkgr setUserInteractionEnabled:YES];
//	[imgvSingupBkgr release];

	//
	[path release];
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BKGR_SIGNUP];
	img = [UIImage imageNamed:path];
	srlvSignupBkgr = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 45, img.size.width, img.size.height)];
	[srlvSignupBkgr setContentSize:CGSizeMake(img.size.width, img.size.height + 120)];
	srlvSignupBkgr.scrollEnabled = NO;
	[srlvSignupBkgr setUserInteractionEnabled:YES];
	[srlvSignupBkgr setBackgroundColor:[UIColor clearColor]];
	srlvSignupBkgr.showsVerticalScrollIndicator = NO;
	UIImageView *imgvSignupBkgr = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
	[imgvSignupBkgr setImage:img];
	[srlvSignupBkgr addSubview:imgvSignupBkgr];
	[imgvSignupBkgr setUserInteractionEnabled:YES];
	[self.view addSubview:srlvSignupBkgr];
	[srlvSignupBkgr release];
	[imgvSignupBkgr release];
	
	
	
	//email
	m_ptxtEmail = [[UITextField alloc] initWithFrame:CGRectMake(120 , 5 + 15, 180, 30)];
	[m_ptxtEmail setFont:[UIFont systemFontOfSize:14]];
	[m_ptxtEmail setTextColor:[UIColor darkGrayColor]];
	[m_ptxtEmail setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[m_ptxtEmail setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
	[m_ptxtEmail setReturnKeyType:UIReturnKeyNext];
	[m_ptxtEmail setAutocorrectionType:UITextAutocorrectionTypeNo];
	m_ptxtEmail.delegate = self;
	[imgvSignupBkgr addSubview:m_ptxtEmail];
	
	// Firstname
	m_ptxtFirstname = [[UITextField alloc] initWithFrame:CGRectMake(120, 45+ 15, 180, 30)];
	[m_ptxtFirstname setFont:[UIFont systemFontOfSize:14]];
	[m_ptxtFirstname setTextColor:[UIColor darkGrayColor]];
	[m_ptxtFirstname setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[m_ptxtFirstname setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
	[m_ptxtFirstname setReturnKeyType:UIReturnKeyNext];
	[m_ptxtFirstname setAutocorrectionType:UITextAutocorrectionTypeNo];
	m_ptxtFirstname.delegate = self;
	[imgvSignupBkgr addSubview:m_ptxtFirstname];
	//Lastname
	m_ptxtLastname = [[UITextField alloc] initWithFrame:CGRectMake(120, 90+ 15, 180, 30)];
	[m_ptxtLastname setFont:[UIFont systemFontOfSize:14]];
	[m_ptxtLastname setTextColor:[UIColor darkGrayColor]];
	[m_ptxtLastname setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[m_ptxtLastname setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
	[m_ptxtLastname setReturnKeyType:UIReturnKeyNext];
	[m_ptxtLastname setAutocorrectionType:UITextAutocorrectionTypeNo];
	m_ptxtLastname.delegate = self;
	[imgvSignupBkgr addSubview:m_ptxtLastname];
	//username
	
	m_ptxtUsername = [[UITextField alloc] initWithFrame:CGRectMake(120, 130+ 15, 180, 30)];
	//[m_ptxtUsername setBackgroundColor:[UIColor redColor]];
	[m_ptxtUsername setFont:[UIFont systemFontOfSize:14]];
	[m_ptxtUsername setTextColor:[UIColor darkGrayColor]];
	[m_ptxtUsername setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[m_ptxtUsername setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
	[m_ptxtUsername setReturnKeyType:UIReturnKeyNext];
	[m_ptxtUsername setAutocorrectionType:UITextAutocorrectionTypeNo];
	m_ptxtUsername.delegate = self;
	[imgvSignupBkgr addSubview:m_ptxtUsername];
	//password
	
	m_ptxtPassword = [[UITextField alloc] initWithFrame:CGRectMake(120, 170+ 20, 180, 30)];
	[m_ptxtPassword setFont:[UIFont systemFontOfSize:14]];
	[m_ptxtPassword setTextColor:[UIColor darkGrayColor]];
	[m_ptxtPassword setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[m_ptxtPassword setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
	[m_ptxtPassword setReturnKeyType:UIReturnKeyNext];
	m_ptxtPassword.secureTextEntry = YES;
	m_ptxtPassword.delegate = self;
	[imgvSignupBkgr addSubview:m_ptxtPassword];
	
	//phone
	
	m_ptxtPhone = [[UITextField alloc] initWithFrame:CGRectMake(120, 200+ 35, 180, 30)];
	[m_ptxtPhone setFont:[UIFont systemFontOfSize:14]];
	[m_ptxtPhone setTextColor:[UIColor darkGrayColor]];
	[m_ptxtPhone setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[m_ptxtPhone setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
	[m_ptxtPhone setAutocorrectionType:UITextAutocorrectionTypeNo];
	[m_ptxtPhone setReturnKeyType:UIReturnKeyNext];
	m_ptxtPhone.delegate = self;
	[imgvSignupBkgr addSubview:m_ptxtPhone];
	
	//picture
	
	m_pBtnTakePhoto = [[UIButton alloc] initWithFrame:CGRectMake(130, 230 +45, 60, 60)];
	[m_pBtnTakePhoto setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",IMG_BTN_TAKEAPHOTO]] forState:UIControlStateNormal];
	[m_pBtnTakePhoto setBackgroundColor:[UIColor clearColor]];
	[m_pBtnTakePhoto addTarget:nil action:@selector(onClickTakePhoto) forControlEvents:UIControlEventTouchUpInside];
	[imgvSignupBkgr addSubview:m_pBtnTakePhoto];
	
	m_pImgUser = [[UIImageView alloc] initWithFrame:CGRectMake(100, 210, 40, 40)];
	//[m_pImgUser setBackgroundColor:[UIColor redColor]];
	
	// Please wait ... 
	_blockerView = [[[UIView alloc] initWithFrame: CGRectMake(0, 0, 200, 60)] autorelease];
	_blockerView.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.8];
	_blockerView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
	_blockerView.alpha = 0.0;
	_blockerView.clipsToBounds = YES;
	if ([_blockerView.layer respondsToSelector: @selector(setCornerRadius:)]) [(id) _blockerView.layer setCornerRadius: 10];
	
	UILabel								*label = [[[UILabel alloc] initWithFrame: CGRectMake(0, 5, _blockerView.bounds.size.width, 15)] autorelease];
	label.text = NSLocalizedString(@"Please Wait…", nil);
	label.backgroundColor = [UIColor clearColor];
	label.textColor = [UIColor whiteColor];
	label.textAlignment = UITextAlignmentCenter;
	label.font = [UIFont boldSystemFontOfSize: 15];
	[_blockerView addSubview: label];
	
	UIActivityIndicatorView				*spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite] autorelease];
	
	spinner.center = CGPointMake(_blockerView.bounds.size.width / 2, _blockerView.bounds.size.height / 2 + 10);
	[_blockerView addSubview: spinner];
	[self.view addSubview: _blockerView];
	[spinner startAnimating];
}
- (void)dimissKeyboard{
	[m_ptxtEmail resignFirstResponder];
	[m_ptxtFirstname resignFirstResponder];
	[m_ptxtLastname resignFirstResponder];
	[m_ptxtUsername resignFirstResponder];
	[m_ptxtPassword resignFirstResponder];
	[m_ptxtPhone resignFirstResponder];
	m_ptxtEmail.text = nil;
	m_ptxtFirstname.text = nil;
	m_ptxtLastname.text = nil;
	m_ptxtUsername.text = nil;
	m_ptxtPassword.text = nil;
	m_ptxtPhone.text = nil;
}
-(void)setKeyBroadActive{
	[m_ptxtEmail becomeFirstResponder];
}

-(void)onClickTakePhoto
{
	if(m_pPhotoPreferenceViewCtrl == nil)
	{
		m_pPhotoPreferenceViewCtrl = [[UIImagePickerController alloc] init]; 
		m_pPhotoPreferenceViewCtrl.delegate = self;
		m_pPhotoPreferenceViewCtrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	}
	[self presentModalViewController:m_pPhotoPreferenceViewCtrl animated:YES];
}
#pragma mark imagePickerController delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
	UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
	if(image)
	{
		m_pImgUser.image = image;
	}
	[self dismissModalViewControllerAnimated:YES];
	
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
	[self dismissModalViewControllerAnimated:YES];
}


-(void)onClickBack
{
	[self dimissKeyboard];
	_blockerView.alpha = 0.0;
	[g_AppDelegate goBackHome:self.view];
}

-(void)onClickDone{
	_blockerView.alpha = 1.0;
	[self requestToServer:m_ptxtUsername.text withPassword:m_ptxtPassword.text withEmail:m_ptxtEmail.text withFirstName:m_ptxtFirstname.text withLastName:m_ptxtLastname.text andAvatarName:@"badges.png"];
	[self performSelector:@selector(resetAllInfo) withObject:nil afterDelay:0.3];
	[self dimissKeyboard];
}

-(void)setUpdateAccount:(BOOL)isUpdateAccount{
	isUpdate = isUpdateAccount;
}

-(void)requestToServer:(NSString *)username withPassword:(NSString *)pass withEmail:(NSString *)email withFirstName:(NSString *)firstName withLastName:(NSString *)lastName andAvatarName:(NSString *)avatarName
{
	NSString *emtyStr = [[NSString alloc] initWithFormat:@""];
	if ([pass isEqualToString:emtyStr] || [email isEqualToString:emtyStr] || [username isEqualToString:emtyStr]) 
	{
		UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Not empty value" message:@"Require all field not empty" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alertView show];
		[alertView release];
	}
	else 
	{
		NSString *post = nil;
		NSData *postData = nil;
		NSString *postLength = nil;
		NSURL *url = nil;
		if(isUpdate == FALSE){
			post = [[NSString alloc] initWithFormat:@"username=%@&password=%@&email=%@&firstname=%@&lastname=%@&avatar=%@",username, pass, email, firstName, lastName, avatarName];
			postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
			postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
			url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/users/signup"];
		}else{
			post = [[NSString alloc] initWithFormat:@"userid=%@&password=%@&email=%@&firstname=%@&lastname=%@&avatar=%@",g_strLoginUserId, pass, email, firstName, lastName, avatarName];
			postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
			postLength = [NSString stringWithFormat:@"%d", [postData length]];
			url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/users/updateprofilebyuserid"];
		}
		
		
		NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
		[theRequest setHTTPMethod:@"POST"];  
		[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
		[theRequest setHTTPBody:postData];	
		
		NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
		if( theConnection )
		{
			m_pWebData = [[NSMutableData data] retain];
		}
		else
		{
			
		}
		
#pragma mark uploadimage to server		
		NSData *imageData = UIImageJPEGRepresentation(m_pImgUser.image, 90);
		// setting up the URL to post to
		NSString *urlString = @"http://iphone.zcentric.com/test-upload.php";
		
		// setting up the request object now
		NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
		[request setURL:[NSURL URLWithString:urlString]];
		[request setHTTPMethod:@"POST"];
		
		/*
		 add some header info now
		 we always need a boundary when we post a file
		 also we need to set the content type
		 
		 You might want to generate a random boundary.. this is just the same
		 as my output from wireshark on a valid html post
		 */
		NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
		NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
		[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
		
		/*
		 now lets create the body of the post
		 */
		NSMutableData *body = [NSMutableData data];
		[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
		[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
		[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
		[body appendData:[NSData dataWithData:imageData]];
		[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
		// setting the body of the post to the reqeust
		[request setHTTPBody:body];
		
		// now lets make the connection to the web
		NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
		NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
		
		NSLog(@"%@",returnString);
	}
}

-(void)resetAllInfo{
	[m_ptxtPassword resignFirstResponder];
	[m_ptxtEmail resignFirstResponder];
	[m_ptxtUsername resignFirstResponder];
	[m_ptxtFirstname resignFirstResponder];
	[m_ptxtLastname resignFirstResponder];
	[m_ptxtPhone resignFirstResponder];
	m_ptxtEmail.text = nil;
	m_ptxtFirstname.text = nil;
	m_ptxtLastname.text = nil;
	m_ptxtPassword.text = nil;
	m_ptxtPhone.text = nil;
	m_ptxtUsername.text = nil;
}
 
#pragma mark delegate connection
#pragma mark delegate connection
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	[m_pWebData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[m_pWebData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot connect to internet" message:@"Please connect to internet!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	
	[alert release];
	[connection release];
	[m_pWebData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSData *loginStatus = [[NSData alloc] initWithBytes: [m_pWebData mutableBytes] length:[m_pWebData length]];
	
	NSString *a = [[NSString alloc] initWithData:loginStatus encoding:NSUTF8StringEncoding];
	NSLog(@"%@",a);
	
	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:loginStatus];
	[xmlParser setDelegate:self];
	
	BOOL success = [xmlParser parse];
	
	if(success)
	{
		NSLog(@"No Errors");
		
	}
	else
	{
		NSLog(@"Error Error Error!!!");
	}
	
	[connection release];
	[m_pWebData release];
}

#pragma mark delegate xmlParser
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
	attributes:(NSDictionary *)attributeDict {
	if(!currentElement) 
		currentElement = [[NSString alloc] initWithString:elementName];
	else
		currentElement = [elementName retain];
	
	NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	NSLog(@"Processing Value: %@", string);
	
	if(!currentElementValue) 
		currentElementValue = [[NSString alloc] initWithString:string];
	else
		currentElementValue = [string retain];
	
	if([currentElement isEqualToString:@"status"])
	{
		if([currentElementValue isEqualToString:@"false"])
		{//
//			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid information" message:@"Email exited" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//			[alert show];
//			
//			[alert release];
//			
//			isContinueParse = false;
		}
		else
		{
			_blockerView.alpha = 0.0;
			[g_AppDelegate openFindFriendsByView:self.view];
		}
	}
	if([currentElement isEqualToString:@"info"])
	{
		_blockerView.alpha = 0.0;
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid information" message:@"Email exits" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		
		[alert release];
		
		isContinueParse = false;
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	
	[currentElement release];
	currentElement = nil;
	[currentElementValue release];
	currentElementValue = nil;
}



#pragma mark UITextFieldDelegate
BOOL move = NO;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
	//keyboardToolbarShouldHide = NO;
	return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	
	//[textField resignFirstResponder];
	if (textField == m_ptxtEmail){
		[m_ptxtFirstname becomeFirstResponder];
	} else if(textField == m_ptxtFirstname)  {
		[m_ptxtLastname becomeFirstResponder];
	} else if (textField == m_ptxtLastname) {
		[m_ptxtUsername becomeFirstResponder];
	} else if (textField == m_ptxtUsername) {
		[m_ptxtPassword becomeFirstResponder];
	} else if (textField == m_ptxtPassword) {
		[m_ptxtPhone becomeFirstResponder];
	}	else if (textField == m_ptxtPhone) {
		[m_ptxtEmail becomeFirstResponder];
	}	
	return YES;
}
//- (void)textFieldDidEndEditing:(UITextField *)textField{
//	
//		if (textField == m_ptxtPhone) {
//			[self setViewMovedUp:NO];
//			move = NO;
//		
//	}else {
//		return;
//	}
//}
#define kOFFSET_FOR_KEYBOARD 60.0
int dem =0;
-(void)textFieldDidBeginEditing:(UITextField *)sender
{

	if (([sender isEqual:m_ptxtEmail] || [sender isEqual:m_ptxtFirstname] || [sender isEqual:m_ptxtLastname]) && self.view.frame.origin.y <0) {
		[self setViewMovedUp:NO];
		move = NO;
	} else if ( ([sender isEqual:m_ptxtPassword] && self.view.frame.origin.y >0 ) ||
			   ([sender isEqual:m_ptxtUsername] && self.view.frame.origin.y >0) ||
				([sender isEqual:m_ptxtPhone]    && self.view.frame.origin.y >0)) {
		[self setViewMovedUp:YES];
		move = YES;
	}
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp 
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5]; // if you want to slide up the view
	
    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
		
			rect.origin.y -= kOFFSET_FOR_KEYBOARD +20;
			rect.size.height += kOFFSET_FOR_KEYBOARD + 20;
		
    }
    else
    {
		
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD +20;
        rect.size.height -= kOFFSET_FOR_KEYBOARD +20;
		
    }
    self.view.frame = rect;
	
    [UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
	srlvSignupBkgr.scrollEnabled = YES;
	NSLog(@"Keyboar will show");
}
- (void)keyboardWillHide: (NSNotification *)notification
{
	srlvSignupBkgr.scrollEnabled = NO;
		NSLog(@"Keyboar will hide");
}
	


- (void)viewWillAppear:(BOOL)animated
{
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
												 name:UIKeyboardWillShowNotification object:nil]; //self.view.window
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];	
}

- (void)viewWillDisappear:(BOOL)animated
{
	// unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
	
}
#pragma mark nextPrevious button
#pragma mark Event touch screen
-(void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
	NSLog(@"Touch begin");
	//UITouch *touch = [[event allTouches] anyObject];
	//CGPoint touchLocation = [touch locationInView:touch.view];  //=> toa do
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
	NSLog(@"Touch move");
}
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
	NSLog(@"Touch end");
	[m_ptxtEmail resignFirstResponder];
	[m_ptxtPassword resignFirstResponder];
	[m_ptxtPhone resignFirstResponder];
	[m_ptxtUsername resignFirstResponder];
	
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	
	[keyboardToolbar release], keyboardToolbar = nil;
	[nextPreviousControl release], nextPreviousControl = nil;
    [super dealloc];
}


@end
